package com.dong.ch04_linkedList;

public class LinkedListDummyHead<E> {
    public class Node<E> {
        E e;
        Node<E> next;

        public Node(E e, Node<E> next) {
            this.e = e;
            this.next = next;
        }
        public Node(E e) {
            this(e,null);
        }
        public Node() {
            this(null,null);
        }
    }
    Node dummyHead;

    public LinkedListDummyHead() {
        dummyHead=new Node();
    }

    int size =0;
    public int getSize() {
        return this.size;
    }
    public boolean isEmpty() {
        return size==0;
    }
    public void addFirst(E e) {
        dummyHead.next=new Node(e,dummyHead.next);
        size++;
    }
    public void add(E e,int pos) {
        if(pos<0||pos>size) {
            throw new IllegalArgumentException("add failed, illegal index");
        }
        Node pre = dummyHead;
        for (int i = 0; i < pos; i++) {
            pre=pre.next;
        }
        pre.next=new Node(e, pre.next);
        size++;
    }
    public void addLast(E e) {
        add(e,size );
    }
    public  E get(int index) {
        if(index<0||index>size) {
            throw new IllegalArgumentException("add failed, illegal index");
        }
        Node<E> cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }
    public E getFirst(){
        return get(0);
    }
    public E getLast() {
        return get(size-1);
    }
    public void set(int index , E e) {
        if(index<0||index>size) {
            throw new IllegalArgumentException("add failed, illegal index");
        }
        Node<E> cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e= e;
    }
    public  boolean contains(E e) {
        Node<E> cur = dummyHead.next;
        for (int i = 0; i < size; i++) {
            if(cur.e.equals(e)) {
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    public E delete(int index) {
        if(index<0||index>size) {
            throw new IllegalArgumentException("add failed, illegal index");
        }
        Node<E> pre = dummyHead;
        for (int i = 0; i < index; i++) {
            pre=pre.next;
        }
        Node<E> cur = pre.next;
        pre.next = cur.next;
        cur.next=null;
        size--;
        return cur.e;
    }
    public E removeFirst() {
        return delete(0);
    }
    public E removeLast() {
        return delete(size-1);
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        Node<E> cur = dummyHead.next;
        while(cur!=null) {
            sb.append(cur.e+"-->");
            cur=cur.next;
        }
        sb.append("NULL");

        return "LinkedListDummyHead{" +
                "data: "+sb.toString()+
                ", size=" + size +
                '}';
    }

    public static void main(String[] args) {
        LinkedListDummyHead<Integer> list = new LinkedListDummyHead<>();
        for (int i = 0; i < 5; i++) {
            list.addFirst(i);
            System.out.println(list);
        }
        list.add(666,2);
        System.out.println(list);
        list.removeFirst();
        System.out.println(list);
        list.removeLast();
        System.out.println(list);
    }
}
